package cn.jsj.cxsys.service.impl;

import org.springframework.stereotype.*;
import cn.jsj.cxsys.dao.*;
import javax.annotation.*;
import cn.jsj.cxsys.service.*;
import java.text.*;
import org.springframework.beans.factory.annotation.*;
import javax.servlet.http.*;
import javax.servlet.*;
import org.springframework.web.multipart.*;
import java.io.*;
import org.apache.commons.io.*;
import org.springframework.mock.web.*;
import cn.jsj.cxsys.pojo.*;
import java.util.*;

@Service
public class ApkDownloadImpl implements ApkDownload
{
    @Resource
    private LabFunction labFunction;
    @Autowired
    private UserService userService;
    @Autowired
    SimpleDateFormat simpleDateFormat;
    @Value("${application.location}")
    private String apklocation;

    @Override
    public void downloadApplication(final HttpSession session, final HttpServletRequest request, final HttpServletResponse response) {
        InputStream inputStream = null;
        ServletOutputStream outputStream = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            inputStream = new FileInputStream(this.apklocation);
            bufferedInputStream = new BufferedInputStream(inputStream);
            outputStream = response.getOutputStream();
            bufferedOutputStream = new BufferedOutputStream((OutputStream)outputStream);
            final byte[] buff = new byte[2048];
            response.setContentType("application/vnd.android.package-archive");
            int bytesRead;
            while (-1 != (bytesRead = bufferedInputStream.read(buff, 0, buff.length))) {
                bufferedOutputStream.write(buff, 0, bytesRead);
            }
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e3) {
            System.out.println("\u8fdc\u7a0b\u4e3b\u673a\u5f3a\u8feb\u5173\u95ed\u4e86\u4e00\u4e2a\u73b0\u6709\u7684\u8fde\u63a5\u3002");
        }
        finally {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                }
                catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();
                }
                catch (IOException e2) {
                    System.out.println("\u8fdc\u7a0b\u4e3b\u673a\u5f3a\u8feb\u5173\u95ed\u4e86\u4e00\u4e2a\u73b0\u6709\u7684\u8fde\u63a5\u3002");
                }
            }
        }
    }

    @Override
    public MultipartFile downloadFile() {
        final File file = new File(this.apklocation);
        FileInputStream input = null;
        MultipartFile multipartFile = null;
        try {
            input = new FileInputStream(file);
            multipartFile = (MultipartFile)new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray((InputStream)input));
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e2) {
            e2.printStackTrace();
        }
        return multipartFile;
    }

    @Override
    public String uploadApplication(final MultipartFile multipartFile, final String account, final String password, final String discription, final String versionName) {
        final String result = this.userService.login(account, password);
        if ("superLader".equals(result)) {
            if (discription == null || discription.trim().length() == 0 || versionName == null || versionName.trim().length() == 0) {
                return "paramLess";
            }
            final File file = new File(this.apklocation);
            if (file.exists()) {
                file.delete();
            }
            try {
                multipartFile.transferTo(file);
                final UpdateInformation updateInformation = new UpdateInformation();
                updateInformation.setDescription(discription);
                updateInformation.setUpdateDatatime(this.simpleDateFormat.format(new Date()));
                updateInformation.setVersion(versionName);
                final int i = this.labFunction.showNewVersion(updateInformation);
                if (i == 1) {
                    return "success";
                }
                return "error";
            }
            catch (IOException e) {
                e.printStackTrace();
                return "error";
            }
        }
        return "checkAccountAndPassword";
    }

    @Override
    public UpdateInformation checkVersion(final String versionName) {
        if (versionName == null || versionName.trim().length() == 0) {
            return null;
        }
        final UpdateInformation updateInformation = this.labFunction.getLastUpdate();
        if (versionName.trim().equals(updateInformation.getVersion())) {
            updateInformation.setID(0);
            return updateInformation;
        }
        return updateInformation;
    }
}
